package com.chj.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.chj.constants.Constants;
import com.chj.dao.OrderDAO;
import com.chj.entity.Order;
import com.chj.mapper.OrderMapper;
import com.chj.mapper.UserMapper;
import com.etc.jdbc.util.JDBCUtils;
@Repository
public class OrderDAOImpl implements OrderDAO{

    @Override
    public int deleteBuOrderId(String orderPk) {
        String sql = " delete from t_order where order_pk = ?";
        return JDBCUtils.update(sql, orderPk);
    }

    @Override
    public boolean isExist(String orderId) {
        String sql = " select count(1) from t_order where order_id = ?";
        int i = JDBCUtils.queryForInt(sql, orderId);
        if ( i > 0) {
            return true;
        } else {
            return false;
        }

    }

    @Override
    public int modify(Order order) {
        String sql = "UPDATE `t_order` SET `pay_status`=?, `pay_date`=? WHERE (`order_pk`=?)";

        return JDBCUtils.update(sql, order.getPayStatus(), order.getPayDate(),order.getOrderPk());
    }

    @Override
    public List<Order> queryOrderByUserId(String userId) {
        String sql = " select * from t_order where user_id = ?";
        
        return JDBCUtils.queryForList(sql, new OrderMapper(), userId);
    }

    @Override
    public List<Order> queryOrderByUserIdWtihCondition(Map<String, Object> queryMap, String UserId) {

        return null;
    }

    @Override
    public int save(Order order) {

        String sql = "INSERT INTO `t_order` (`order_pk`, `order_id`, `user_id`, `ecar_id`, `network_id`, `park_id`, `park_price`, `park_pay_money`, `order_status`, `complete_status`, `pay_status`, `pay_date`, `order_begin_time`, `order_end_time`, `order_time_long`, `order_paysuccess_id`, `order_callback_id`, `create_date`, `modify_date`, `delete_date`, `status`)";
               sql += "VALUES               (?         , ?         , ?       , ?        , ?           , ?        , ?           , ?               , ?             , ?                , ?           , ?         , ?                 , ?               , ?                , ?                    , ?                  , ?            , ?            , ?            , ?)";
        return JDBCUtils.update(sql, order.getOrderPk(), order.getOrderId(), order.getUserId(), order.getEcarId(), order.getNetworkId(), order.getParkId(), order.getParkPrice(), order.getParkPayMoney(), order.getOrderStatus(),order.getCompleteStatus(), order.getPayStatus(),order.getPayDate(), order.getOrderBeginTime(),order.getOrderEndTime(), order.getOrderTimeLong(),order.getOrderPaysuccessId(), order.getOrderCallbackId(),order.getCreateDate(), order.getModifyDate(), order.getDeleteDate(), order.getStatus());
    }

    @Override
    public boolean queryECarStatus(String ecarId, Date newDate) {

        String sql = "select count(1) from t_order t where t.order_begin_time < ? and t.order_end_time > ? and t.ecar_id = ?";
        int queryForInt = JDBCUtils.queryForInt(sql, newDate, newDate, ecarId);
        
        if (queryForInt > 0) {
            return true;
        } else {
            return false;
        }
    }

	@Override
	public boolean queryParkStatus(String networkId, String parkId, String chargeTimeBegin, String chargeTimeEnd) {

		String sql = "select count(1) from t_order t where t.network_id = ? and t.park_id = ? and t.order_begin_time >= ? and t.order_end_time <= ? and t.pay_status = 1";
		int size = JDBCUtils.queryForInt(sql, networkId, parkId, chargeTimeBegin, chargeTimeEnd);

		if (size > 0) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public Date queryParkWaitTime(String networkId
							    , String parkId
							    , String chargeTimeBegin
							    , String chargeTimeEnd) {
		String sql = "select t.order_end_time  from t_order t where t.network_id =? and t.park_id = ? and t.order_begin_time >= ? and t.order_end_time <= ? and t.pay_status = 1 order by t.order_end_time desc limit 0,1";

		//最近一笔订单的结束时间
		return JDBCUtils.queryForDate(sql, networkId, parkId, chargeTimeBegin, chargeTimeEnd);
	}

    @Override
    public Order get(String orderId) {

        String sql = " select * from t_order where order_pk = ?";

        return JDBCUtils.queryForObject(sql, new OrderMapper(), orderId);
    }

    @Override
    public List<Order> getOrderByUserIdByNow(String userPk, String dateString) {
        String sql = " select * from t_order where user_id = ? and order_begin_time >= ?";
        
        return JDBCUtils.queryForList(sql, new OrderMapper(), userPk,dateString);
    }

    @Override
    public List<Order> getOrderByUserIdByNowOver(String userPk,String dateString) {

        String sql = " select * from t_order where user_id = ? and order_begin_time < ?";
        
        return JDBCUtils.queryForList(sql, new OrderMapper(), userPk,dateString);
    }

    @Override
    public List<Order> getOrderListBy(String userName
                                    , String orderId
                                    , String createDateFrom
                                    , String createDateTo
                                    , int page
                                    , int rows) {
        List<Object> paramsList = new ArrayList<Object>();

        String sql = "";

        sql += " select *                         ";
        sql += "   from t_order t                 ";
        sql += "  where t.status = 1              ";

        if (StringUtils.isNotBlank(userName)) {
            sql += " and t.user_id like ? ";
            paramsList.add(userName+Constants.SQL_LIKE);
        }

        if (StringUtils.isNotBlank(orderId)) {
            sql += " and t.order_id like ? ";
            paramsList.add(orderId + Constants.SQL_LIKE);
        }

        if (StringUtils.isNotBlank(createDateFrom)) {
            sql += " and t.create_date > ? ";
            paramsList.add(createDateFrom);
        }
        if (StringUtils.isNotBlank(createDateTo)) {
            sql += " and t.create_date < ? ";
            paramsList.add(createDateTo);
        }

        sql += " limit ?,?";

        paramsList.add(((page-1)*rows));
        paramsList.add((page)*rows);

        //定义数组长度
        int paramsLength = 0;
        if(paramsList.size() > 0) {
            paramsLength = paramsList.size();
        }

        //创建数组
        Object[] params = new Object[paramsLength];

        //赋值给数组
        for (Object object : paramsList) {
            int paramsIndex = paramsList.indexOf(object);
            params[paramsIndex] = object;
        }

        return JDBCUtils.queryForList(sql, new OrderMapper(), params);
    }

    @Override
    public int getOrderListBy(String userName
                            , String orderId
                            , String createDateFrom
                            , String createDateTo) {

        List<Object> paramsList = new ArrayList<Object>();

        String sql = "";

        sql += " select count(1)                  ";
        sql += "   from t_order t                 ";
        sql += "  where t.status = 1              ";

        if (StringUtils.isNotBlank(userName)) {
            sql += " and t.user_id like ? ";
            paramsList.add(userName+Constants.SQL_LIKE);
        }

        if (StringUtils.isNotBlank(orderId)) {
            sql += " and t.order_id like ? ";
            paramsList.add(orderId + Constants.SQL_LIKE);
        }

        if (StringUtils.isNotBlank(createDateFrom)) {
            sql += " and t.create_date > ? ";
            paramsList.add(createDateFrom);
        }
        if (StringUtils.isNotBlank(createDateTo)) {
            sql += " and t.create_date < ? ";
            paramsList.add(createDateTo);
        }

        //定义数组长度
        int paramsLength = 0;
        if(paramsList.size() > 0) {
            paramsLength = paramsList.size();
        }

        //创建数组
        Object[] params = new Object[paramsLength];

        //赋值给数组
        for (Object object : paramsList) {
            int paramsIndex = paramsList.indexOf(object);
            params[paramsIndex] = object;
        }

        return JDBCUtils.queryForInt(sql, params);
    }

    @Override
    public boolean queryECarStatusBy(String ecarPk
                                   , String chargeTimeBegin
                                   , String chargeTimeEnd) {

        String sql = "select count(1) from t_order t where t.order_begin_time <= ? and t.order_end_time >= ? and t.ecar_id = ?";
        int queryForInt = JDBCUtils.queryForInt(sql, chargeTimeBegin, chargeTimeEnd, ecarPk);
        if (queryForInt > 0) {
            return true;
        } else {
            return false;
        }
    }

}
